package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPaymentDO;
import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicPaymentMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicProductCodeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicProductCodeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 产品编码 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicProductCodeServiceImpl  extends ServiceImpl<BasicProductCodeMapper, BasicProductCodeDO> implements BasicProductCodeService {

    @Resource
    private BasicProductCodeMapper basicProductCodeMapper;

    @Override
    public Long createBasicProductCode(BasicProductCodeSaveReqVO createReqVO) {
        // 插入
        BasicProductCodeDO basicProductCode = BeanUtils.toBean(createReqVO, BasicProductCodeDO.class);
        validateUnique(basicProductCode);
        basicProductCodeMapper.insert(basicProductCode);
        // 返回
        return basicProductCode.getId();
    }

    @Override
    public void updateBasicProductCode(BasicProductCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicProductCodeExists(updateReqVO.getId());
        // 更新
        BasicProductCodeDO updateObj = BeanUtils.toBean(updateReqVO, BasicProductCodeDO.class);
        validateUnique(updateObj);
        basicProductCodeMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicProductCode(Long id) {
        // 校验存在
        validateBasicProductCodeExists(id);
        // 删除
        basicProductCodeMapper.deleteById(id);
    }

    private void validateBasicProductCodeExists(Long id) {
        if (basicProductCodeMapper.selectById(id) == null) {
            throw exception(BASIC_PRODUCT_CODE_NOT_EXISTS);
        }
    }
    @VisibleForTesting
    void validateUnique(BasicProductCodeDO data) {
        long count = basicProductCodeMapper.selectCount(new LambdaQueryWrapper<BasicProductCodeDO>().eq(BasicProductCodeDO::getCode,data.getCode()).ne(data.getId()!=null,BasicProductCodeDO::getId,data.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("产品编码已存在");
        }
    }

    @Override
    public BasicProductCodeDO getBasicProductCode(Long id) {
        return basicProductCodeMapper.selectById(id);
    }

    @Override
    public PageResult<BasicProductCodeDO> getBasicProductCodePage(BasicProductCodePageReqVO pageReqVO) {
        return basicProductCodeMapper.selectPage(pageReqVO);
    }

}